import { Component, OnInit, Output, EventEmitter } from "@angular/core";
import {Md5} from "ts-md5/dist/md5";
import { UserService } from "./../user.service";

// import toolTip from "../../util/tooltip.js";

import { CHANGEPWDSMS } from "./../../shared";

interface FindUser {
  mobile: string;
  passwd: string;
  smsCode: string;
  smsToken: string;
}

@Component({
  selector: "user-findpwd",
  templateUrl: "findpwd.component.html",
  styleUrls: ["findpwd.component.less"]
})
export class UserFindpwdComponent implements OnInit {
  @Output() setModal = new EventEmitter();
  errText: string;
  waitTime: number = 0;
  findUser: FindUser = {
    mobile: "",
    passwd: "",
    smsCode: "",
    smsToken: ""
  };
  repassword: string;
  flag: boolean = true;
  constructor(private userService: UserService) { }

  ngOnInit() {}

  next() {
    // if (this.findUser.smsToken !== Md5.hashStr(this.findUser.smsCode + ":" + this.findUser.mobile)) {
    //   this.errText = "验证码输入不正确";
    //   return;
    // } else {
    if(this.checkInfoValid() && this.smsCodeValidCheck() && this.checkSmsTokenValid()) {
      this.flag = false;
    }
      
    // }
  }

  // 重置密码
  resetPwd() {
    if (this.findUser.passwd && this.repassword) {
      this.errText = "";
      if (!this.checkPwdValid()) {
        return;
      }
      if (this.findUser.passwd !== this.repassword ) {
        this.errText = "两次密码输入不一致";
        return;
      }
  
      this.userService.changePwd(this.findUser).subscribe((data) => {
        if ( data.code !== 0 ) {
          this.errText = data.msg;
        } else {
        this.errText = "重置密码成功,请使用新密码登录";
        }
      }, (err) => {
        console.log("reset pwd failed:", err);
        this.errText = "服务连接失败，请稍后重试！";
      });
    }
  }

  checkPwdValid() {
    let val = true;
    if ( !this.findUser.passwd) {
      this.errText = "请输入密码";
      val = false;
    }

    let fmt = /^[a-zA-Z0-9]{6,20}$/;
    if (!fmt.test(this.findUser.passwd)) {
      this.errText = "输入6-20位由字母、数字";
      val = false;
    }
    return val;
  }

  checkInfoValid() {
    let val = true;
    // 校验手机号是否可用
    if (!this.findUser.mobile) {
      // this.errText = "手机号码不能为空";
      val = false;
    } else {
      // let fmt = /^\d{11}$/;
      let fmt = /^1[3578]\d{9}$/;
      if (!fmt.test(this.findUser.mobile)) {
        // this.errText = "手机号格式不正确";
        val = false;
      }
    }
    return val;
  }
  
  checkSmsTokenValid() {
    this.errText = "";
    let val = true;
    if ( !this.findUser.smsToken || this.findUser.smsToken === "") {
      val = false;
      this.errText = "请获取验证码";
    }
    return val;
  }
  // 验证码设定检测
  smsCodeValidCheck() {
    let val = true;
    if (!this.findUser.smsCode) {
      val = false;
    }
    else {
      let fmt = /^[\d\w]{6}$/;
      if (!fmt.test(this.findUser.smsCode)) {
        val = false;
      }
    }
    return val;
  }


  sendSmsCode() {
    this.errText = "";
    if (!this.checkInfoValid()) {
      this.errText = "手机号码格式不正确！";
      return;
    }
    this.userService.sendSmsCode( `${CHANGEPWDSMS}`, this.findUser.mobile).subscribe((data) => {
      if ( data.code !== 0 ) {
        this.errText = data.msg;
      } else {
        this.errText = "";
        this.findUser.smsToken = data.data.smsToken;
        this.waitTime = 60;
        let t = setInterval(() => {
          this.waitTime--;
          if (this.waitTime <= 0) {
            clearInterval(t);
          }
        }, 1000);
      }
    }, (err) => {
      console.log("sms/otps failed data:", err);
      this.errText = "服务连接失败，请稍后重试！";
    });
  }

  changeModal(action: string) {
    if (action === "hide" && !this.userService.isToken) {
    } else {
      this.errText = "";
      this.setModal.emit(action);
    }
  }
}
